package org.iabako.client.ui.form.business.conversation;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.tools.IabakoAsyncCallback;
import org.iabako.client.ui.form.AbstractForm;
import org.iabako.client.ui.form.business.client.ClientForm;
import org.iabako.client.ui.layout.field.*;
import org.iabako.client.ui.page.business.conversation.ConversationModify;
import org.iabako.client.user.AuthenticateUserProvider;
import org.iabako.shared.entity.business.AbstractEntity;
import org.iabako.shared.entity.business.Client;
import org.iabako.shared.entity.business.ClientConversation;
import org.iabako.shared.entity.enumeration.ConversationStatus;
import org.iabako.shared.entity.enumeration.ConversationType;
import org.iabako.shared.entity.enumeration.TrackingType;
import org.iabako.shared.iservice.business.ClientConversationServiceAsync;
import org.iabako.shared.tools.GenericTools;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by jose on 12/02/15.
 */
public abstract class ConversationForm extends AbstractForm {

    interface MyUiBinder extends UiBinder<HTMLPanel, ConversationForm> {
    }

    protected final ClientConversationServiceAsync clientConversationServiceAsync = ClientConversationServiceAsync.Util.getInstance();

    protected MyUiBinder binderPaymentForm = GWT.create(MyUiBinder.class);
    @UiField
    SelectList type;
    @UiField
    InputTextDate date;
    @UiField
    InputText interlocutor;
    @UiField
    InputTextArea body;
    @UiField
    RadioButtonList status;
    @UiField
    Button addOrUpdateAction;
    @UiField
    LabelText title;
    @UiField
    InputText subject;

    public ConversationForm() {
        simplePopup = true;
        setForm(binderPaymentForm, null, null);

        if (this instanceof ConversationModify) {
            title.setLabel("conversation_form_modify_title");
            addOrUpdateAction.setLabel("action_modify");
        }
        date.setValue(new Date());

        addOrUpdateAction.getButton().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                validate();
            }
        });
    }

    protected void setClient(Client client) {
        interlocutor.setValue(client.getName());
    }

    @Override
    protected List<IField> getMandatoryFields() {
        return Arrays.asList((IField) type, date, interlocutor, subject, body, status);
    }

    protected void setConversation(ClientConversation conversation) {

        entity = conversation;

        type.setEnumValue(conversation.getType());
        date.setValue(GenericTools.getDateFromString(conversation.getDateString()));
        interlocutor.setValue(conversation.getInterlocutor());
        subject.setValue(conversation.getSubject());
        body.setValue(conversation.getBody());
        status.setValue(conversation.getStatus());

        if (conversation.isAutomatic()) {
            type.setEnabled(false);
            date.setEnabled(false);
            interlocutor.setEnabled(false);
            subject.setEnabled(false);
            status.setEnabled(false);
            body.setEnabled(false);

            addOrUpdateAction.getButton().setVisible(false);
        }
    }

    @Override
    protected AbstractEntity getEntity() {

        if (entity == null) {
            entity = new ClientConversation();
        }

        ClientConversation conversation = (ClientConversation) entity;
        conversation.setType((ConversationType) type.getEnumValue());
        conversation.setDate(date.getDateValue());
        conversation.setDateString(GenericTools.formatDateToString(date.getDateValue()));
        conversation.setInterlocutor(interlocutor.getValue());
        conversation.setSubject(subject.getValue());
        conversation.setBody(body.getValue());
        conversation.setStatus((ConversationStatus) status.getValue());
        conversation.setClient(((ClientForm) parentForm).getClient());

        return entity;
    }

    @Override
    public boolean validate() {
        if (!uiValidation()) {
            return false;
        }
        ClientConversation conversation = (ClientConversation) serverValidation();
        if (conversation == null) {
            return false;
        }

        clientConversationServiceAsync.save(conversation, new IabakoAsyncCallback<ClientConversation>() {

            public void onSuccess(ClientConversation result) {
                popup.handleClickClose();

                ((ClientForm) parentForm).clientConversationsForm.conversationTable.refreshSearch();
                AuthenticateUserProvider.getTrackingPanel().refresh(TrackingType.conversationNew, TrackingType.conversationModify);
                super.onSuccess(result);
            }
        });

        return true;
    }
}
